package cn.livingCloud.entity.core;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.entity.user.UserAccount;
import cn.livingCloud.repository.*;
import cn.livingCloud.utils.UuidGenerator;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门
 */
public class Dept extends BaseEntity {

    private transient DeptMapper mapper = BeanProvider.getBean(DeptMapper.class);
    private transient EmployeeMapper employeeMapper = BeanProvider.getBean(EmployeeMapper.class);
    private transient EmployeeAccountMapper employeeAccountMapper = BeanProvider.getBean(EmployeeAccountMapper.class);
    private transient EmployeeWechatMapper employeeWechatMapper = BeanProvider.getBean(EmployeeWechatMapper.class);
    private transient UserAccountMapper userAccountMapper = BeanProvider.getBean(UserAccountMapper.class);

    private String uuid = UuidGenerator.generate();

    private String name;

    private String parentUuid;

    public Dept() {
    }

    public String getUuid() {
        return uuid;
    }

    public Dept setUuid(String uuid) {
        this.uuid = uuid;
        return this;
    }

    public String getName() {
        return name;
    }

    public Dept setName(String name) {
        this.name = name;
        return this;
    }

    public String getParentUuid() {
        return parentUuid;
    }

    public Dept setParentUuid(String parentUuid) {
        this.parentUuid = parentUuid;
        return this;
    }

    //删除父类和子类
    public void archive() {
        this.setArchive(true);
        mapper.updateByPrimaryKey(this);
        List<Employee> listByDeptUuid = employeeMapper.getListByDeptUuid(this.getUuid());
        for (Employee employee : listByDeptUuid) {
            //员工注销
            employee.setArchive(true);
            employeeMapper.updateByPrimaryKey(employee);
            //员工账号注销
            List<UserAccount> accountListByEmployee = employeeAccountMapper.findAccountListByEmployee(employee.getUuid());
            for (UserAccount userAccount : accountListByEmployee) {
                userAccount.setEnable(false);
                userAccountMapper.updateEnable(userAccount);
            }
            //解绑微信信息
            List<EmployeeWechat> byEmployeeUuid = employeeWechatMapper.findByEmployeeUuid(employee.getUuid());
            for (EmployeeWechat employeeWechat : byEmployeeUuid) {
                employeeWechat.setArchive(true);
                employeeWechatMapper.updateByPrimaryKey(employeeWechat);
            }
        }

        List<Dept> listChildDept = mapper.findListChildDept(this.uuid);
        for (Dept dept : listChildDept) {
            dept.archive();
        }
    }

    public ArrayList<String> getAllSubDeptUuid() {
        ArrayList<String> depts = new ArrayList<>();
        depts.add(this.uuid);
        List<Dept> sublist = getSubDept();
        for (Dept dept : sublist) {
            ArrayList<String> allSubDeptUuid = dept.getAllSubDeptUuid();
            depts.addAll(allSubDeptUuid);
        }
        return depts;
    }

    private List<Dept> getSubDept() {
        return mapper.findListChildDept(this.uuid);
    }
}